from bs4 import BeautifulSoup
import bs4
import lxml
import requests

html = """
<html><head><title>The Dormouse's story</title>
</head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were><a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a><a href="http://example.com/lacie" class="sister" id="link2">Lacie</a><a href="http://example.com/tillie" class="sister" id="link3">Tillie</a></p>
<p class="story">...</p>
</body>
</html>
"""


def soupType(soup):
    '''查看 soup 的类型'''
    print(type(soup))
    print(type(soup.a))

def soupTag(soup):
    '''Tag 相关的操作'''
    print(soup.head)
    print(soup.title.text)
    # print(soup.body)
    # print(soup.a) # 相同tag 下只提取最近的那个

    # # 学习 tag name 相关的属性操作
    # print(soup.name) #　这个是BeautifulSoup对象的name
    # print(soup.a.name) # 这个就是本生tag的名字
    # print(soup.body.name)

    # # 学习 tag attrs 相关的属性操作
    # print(soup.attrs)
    # print(soup.title.attrs)
    # print(soup.p.attrs)

    # print(soup.a.attrs)
    # print(soup.a.attrs.get('href'))
    # print(soup.a.attrs['href'])
    # print(soup.a['href'])
    # print(soup.a.get('href'))

    # # 对属性进行修改以及删除
    # soup.a['href']="caobinxin"
    # print(soup.a.get('href'))
    # del soup.a['href']
    # print(soup.a.get('href'))
    
def navigableString(soup):
    '''获取指定标签中的内容'''
    
    print(soup.p)

    print(soup.p.string)
    print(type(soup.p.string))

def beautifulsoup(soup):
    '''对　BeautifulSoup　对象本身进行操作'''
    print(soup.name)
    print(soup.attrs)
    print(type(soup.name))

def commentDemo(soup):
    """输出注释的内容"""
    print(soup.a)
    print(soup.a.string)
    print(type(soup.a.string))

    if type(soup.a.string) == bs4.element.Comment:
        print(soup.a.string)

def directChildNode(soup):
    '''遍历直接子节点'''
    # print(soup.head.contents)
    # print(soup.head.contents[0])

    # print(soup.html.contents[4])
    # print(soup.body.contents)

    print(soup.head.children)
    for item in soup.body.children:
        print(item)

def allChildNode(soup):
    for item in soup.descendants:
        print(item)

if __name__ == "__main__":
    soup = BeautifulSoup(html, 'lxml')
    
    # soupType(soup)
    soupTag(soup)
    # navigableString(soup)
    # beautifulsoup(soup)
    # commentDemo(soup)
    # directChildNode(soup)
    # allChildNode(soup)

    # for item in soup.find_all('p'):
    #     print(item)

    # for item in soup.find_all(['a', 'p']):
    #     print(item)

    # for item in soup.find_all(True):
    #     print(item)
    

